﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using SSCAI.Common;
using SSCAI.Client.ViewModels;
using SSCAI.Model;

namespace SSCAI.Client
{
    /// <summary>
    /// Interaction logic for SnWindow.xaml
    /// </summary>
    public partial class SnWindow : Window
    {
        SnWindowViewModel vm = new SnWindowViewModel();

        public SnWindow()
        {
            InitializeComponent();

            for (int i = 1; i <= 600; i++)
            {
                this.cmbDays.Items.Add(i);
            }

            this.cmbDays.SelectedValue = 7;

            this.GenerateSns();

            //this.dgSn.ItemsSource = vm.SnsProperty;
        }

        private void GenerateSns()
        {
            vm.SnsProperty = new List<SnModel>();
            string username = string.Empty;
            for (int i = 0; i < 100; i++)
            {
                vm.SnsProperty.Add(
                    new SnModel
                    {
                        Id = i + 1,
                        ExpiredDate = DateTime.Now.AddDays((int)this.cmbDays.SelectedValue).ToShortDateString(),
                        Sn = CalculateHelper.GenerateSn(DateTime.Now.AddDays((int)this.cmbDays.SelectedValue), out username),
                        UserName = username
                    });
            }

            this.dgSn.Dispatcher.Invoke(new Action(delegate() { this.dgSn.ItemsSource = vm.SnsProperty; }));
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.GenerateSns();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var s in vm.SnsProperty)
            {
                sb.Append("sscai-" + s.UserName + "," + s.ExpiredDate + "," + s.Sn + "\r\n");
            }

            var sbs = sb.ToString();

            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
            dlg.FileName = "SSCaiSn"; // Default file name
            dlg.DefaultExt = ".csv"; // Default file extension
            dlg.Filter = "csv documents (.csv)|*.csv"; // Filter files by extension


            Nullable<bool> result = dlg.ShowDialog();
            // Process save file dialog box results
            if (result == true)
            {
                // Save document
                string filename = dlg.FileName;

                if (!System.IO.File.Exists(filename))
                {
                    using (System.IO.StreamWriter sw = System.IO.File.CreateText(filename))
                    {
                        sw.Write(sbs);
                    }
                }
                else
                {
                    using (System.IO.StreamWriter sw = System.IO.File.CreateText(filename))
                    {
                        sw.Write(sbs);
                    }

                }
            }
        }
    }
}
